package vcp.server.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

import vcp.entity.PeriodicReport;
import vcp.enums.ReportType;
import vcp.enums.SiteEnum;
import vcp.server.DBAdapter;
import vcp.server.DAO.interfaces.IGenericDAO;
import vcp.server.utility.Constants.TableNames;
import vcp.server.utility.Constants.PeriodicReportTable;
import vcp.server.utility.SQLQueries;

/**
 * 
 * @author VCP team
 * <p> periodic reports DAO implementing relevant queries </p> 
 */
public class PeriodicReportDAO implements IGenericDAO<PeriodicReport, Void>
{

	// ****************************************************************************************************************************
	// IGenericDAO methods
	// ****************************************************************************************************************************

	@Override
	public Integer findMaxID()
	{
		Integer reportID = null;
		String query =
				String.format(
						SQLQueries.SELECT_MAX__VALUE__AS__NAME__FROM__TABLE_NAME__
						, PeriodicReportTable.ID
						, SQLQueries.MAX_ID
						, TableNames.PERIODIC_REPORT);

		try
		{
			ResultSet set = DBAdapter.getAdapter().find(query);
			if (set.next())
			{
				reportID = set.getInt("maxID");
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}

		if (reportID == null)
		{
			reportID = 0;
		}

		return ++reportID;
	}

	@Override
	public PeriodicReport create()
	{
		return new PeriodicReport();
	}

	@Override
	public PeriodicReport findByEntity(Void entity)
	{
		return null;
	}

	@Override
	public PeriodicReport findByID(int ID)
	{
		return this.findByID(String.valueOf(ID));
	}

	@Override
	public PeriodicReport findByID(String ID)
	{
		String query =
				String.format(
						SQLQueries.SELECT_ALL_FROM__SINGLETABLE__WHERE__COMPARE_
						, TableNames.PERIODIC_REPORT
						, PeriodicReportTable.ID + "=" + ID);

		PeriodicReport periodicReport = null;
		try
		{
			ResultSet set = DBAdapter.getAdapter().find(query);
			if (set.next())
			{
				periodicReport = this.create();

				periodicReport.setID(set.getInt(PeriodicReportTable.ID));
				periodicReport.setType(ReportType.convert(set.getString(PeriodicReportTable.TYPE)));
				periodicReport.setStartDate(new DateTime(set.getTimestamp(PeriodicReportTable.START_DATE)));
				periodicReport.setEndDate(new DateTime(set.getTimestamp(PeriodicReportTable.END_DATE)));
				periodicReport.setSite(SiteEnum.convert(set.getString(PeriodicReportTable.SITE)));
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}

		return periodicReport;
	}

	@Override
	public List<PeriodicReport> findAll(Void info)
	{
		String query =
				String.format(
						SQLQueries.SELECT_ALL_FROM_SINGLETABLE
						, TableNames.PERIODIC_REPORT);

		List<PeriodicReport> listOfPeriodic = null;
		try
		{
			ResultSet set = DBAdapter.getAdapter().find(query);
			listOfPeriodic = new ArrayList<>();
			while (set.next())
			{
				PeriodicReport periodicReport = this.create();

				periodicReport.setID(set.getInt(PeriodicReportTable.ID));
				periodicReport.setType(ReportType.convert(set.getString(PeriodicReportTable.TYPE)));
				periodicReport.setStartDate(new DateTime(set.getTimestamp(PeriodicReportTable.START_DATE)));
				periodicReport.setEndDate(new DateTime(set.getTimestamp(PeriodicReportTable.END_DATE)));
				periodicReport.setSite(SiteEnum.convert(set.getString(PeriodicReportTable.SITE)));

				listOfPeriodic.add(periodicReport);
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}

		return listOfPeriodic;
	}

	@Override
	public boolean save(PeriodicReport entity)
	{
		boolean result = false;

		String valuesSet =
				String.format(
						"%d,\"%s\",\"%s\",\"%s\",\"%s\""
						, entity.getID()
						, entity.getType().toString()
						, entity.getStartDate() != null
						? new Timestamp(entity.getStartDate().getMillis())
						: new Timestamp(new DateTime(2001, 1, 1, 0, 0, 0).getMillis())
						, entity.getEndDate() != null
						? new Timestamp(entity.getEndDate().getMillis())
						: new Timestamp(new DateTime(2001, 1, 1, 0, 0, 0).getMillis())
						, entity.getSite().toString());

		String sqlQuery =
				String.format(
						SQLQueries.INSERT_INTO__TABLENAME__VALUES__
						, TableNames.PERIODIC_REPORT
						, valuesSet);

		try
		{
			result = DBAdapter.getAdapter().save(sqlQuery);
		} catch (SQLException e)
		{
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public boolean refresh(String sqlQuery)
	{
		return false;
	}

	@Override
	public boolean deleteByID(int ID)
	{
		return false;
	}

	@Override
	public boolean deleteByID(String ID)
	{
		return false;
	}

}
